package com.by.common.dao;

import com.by.common.dao.bean.Page;
import com.by.common.dao.bean.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;

@SuppressWarnings("all") //清空警告
public abstract class MyBaseServiceImpl<T,M extends MyBaseMapper> implements MyBaseService<T> {
    @Autowired
    protected M baseMapper;


    @Override
    public int saveEntity(T entity) {
        return baseMapper.insert(entity);
    }

    @Override
    public int updateEntity(T entity) {
        return baseMapper.updateById(entity);
    }

    @Override
    public int deleteEntity(Serializable id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public T getEntity(Serializable id) {
        return (T) baseMapper.selectById(id);
    }

    @Override
    public <P extends Page> PageResult<T> query(P condition) {
        //分页
        PageHelper.startPage(condition.getPage(), condition.getPageSize());
        //紧跟查询语句
        List list = baseMapper.query(condition);
        //分页结果
        PageInfo info = new PageInfo(list);
        return new PageResult<T>(condition.getPage(), condition.getPageSize(), info.getTotal(), list);
    }
}
